using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using buildingLib.Prepare;

namespace _dmis.prepare
{
    public partial class AreaWholeDlg : _dmis.UIFormBase
    {
        AreaWhole m_area;
        public AreaWholeDlg()
        {
            InitializeComponent();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            this.areaWholeBindingSource.RaiseListChangedEvents = false;
            this.areaWholeBindingSource.EndEdit();

            try
            {
                m_area.Save();
                ProjectList.GetProjects().Insert(m_area.Location, m_area.ID);
            }
            catch (Csla.DataPortalException ex)
            {
                MessageBox.Show(ex.BusinessException.ToString(),
                  "Error saving", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                  "Error Saving", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            finally
            {
                this.areaWholeBindingSource.RaiseListChangedEvents = true;
                this.areaWholeBindingSource.ResetBindings(false);
            }
        }

        private void AreaWholeDlg_Load(object sender, EventArgs e)
        {
            m_area = AreaWhole.NewAreaWhole();
            areaWholeBindingSource.DataSource = m_area;
            projectListBindingSource.DataSource = ProjectList.GetProjects();
        }

        private void btn_New_Click(object sender, EventArgs e)
        {
            
            //this.areaWholeBindingSource.RaiseListChangedEvents = false;
            this.areaWholeBindingSource.EndEdit();
            m_area = AreaWhole.NewAreaWhole();
            this.areaWholeBindingSource.DataSource = m_area;
            //this.areaWholeBindingSource.RaiseListChangedEvents = true;
            this.areaWholeBindingSource.ResetBindings(false);
        }

        private void projectListComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if(this.projectListComboBox.SelectedValue !=null)
            {
                this.areaWholeBindingSource.EndEdit();
                m_area = AreaWhole.GetAreaWhole((Guid)this.projectListComboBox.SelectedValue);
                this.areaWholeBindingSource.DataSource = m_area;
                //this.areaWholeBindingSource.RaiseListChangedEvents = true;
                this.areaWholeBindingSource.ResetBindings(false);
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            AreaWhole.DeleteAreaWhole(m_area.ID);
            ProjectList.InvalidateCache();
            projectListBindingSource.DataSource = ProjectList.GetProjects();            
        }
    }
}

